<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 2020/12/10
 * Time: 15:31
 */

namespace app\common\model;
use think\Db;
use think\Model;

class AddressModel extends Model
{
    protected $name = 'users_address';
    public static function  add($arr){
        $se = new self();
        $arr['create_time'] = date("Y-m-d H:i:s");
        return $se->allowField(true)->replace()->save($arr,'');
    }
    public function findRegion($latitude,$longitude){
        $list =  Db::name('region')
            ->where('open',1)
            ->field('name,id as region_id,open')->order('id','desc')->select();
        $in = '';
        foreach ($list as $v){
            $in.=$v['region_id'].',';
        }
        $location = Db::name('region_location')->wherein('region_id',$in)->field('longitude as lng,latitude as lat,region_id')->select();
        foreach ($list as $key=>$val){
            foreach ($location as $v){
                if($v['region_id'] ==$val['region_id']){
                    $list[$key]['location'][] = $v;

                }
            }
        }

        $array = ["lat"=>floatval($latitude),"lng"=>floatval($longitude)];//进行验证的区域

        foreach ($list as $key=>$val) {

            validationMap::setCoordArray($val['location']); //设置地图点
            if (validationMap::isCityCenter($array)) {
                return $list[$key];
            }
        }
        return false;
    }
    public function getRegionList($is_open = false){
        $where =[];
        if($is_open !==false){
            $where []= ['open','=',1];
        }
        return Db::name('region')->where($where)->field('name,id')->select();


    }
}